package com.xxxt.zpc.controller;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.xxxt.entity.Feedback;
import com.xxxt.entity.Inform;
import com.xxxt.zpc.service.FeedbackService;
import com.xxxt.zpc.service.InformService;

/**
 * 通知的查询显示发布修改
 * 
 * @author 星梦
 *
 */
@RequestMapping("inform")
@Controller
public class InformController {
	@Autowired
	private HttpSession session;
	@Autowired
	private InformService informService;
	@Autowired
	private FeedbackService feedbackService;

	/*
	 * 发布通知
	 */
	@PostMapping("/insert")
	@ResponseBody
	public JSONObject insertInform(String title, String content) {
		JSONObject json = new JSONObject();
		int status = -1;
		String message = "通知发布失败,请重试";
		Boolean result = false;
		String identity = (String) session.getAttribute("identity");
		String account = (String) session.getAttribute("account");
		if (identity.equals("teacher")) {// 职位是老师，则给他发布

			result = informService.insertInform(title, content, account);

		} else {
			message = "老师才可以发布通知";
		}

		if (result) {
			status = 200;
			message = "通知发布成功";
		}
		json.put("status", status);
		json.put("message", message);
		return json;
	}

	/*
	 * 显示单条通知
	 */
	@PostMapping("/selectInformById")
	@ResponseBody
	public JSONObject selectInformById(String informID) {
		JSONObject json = new JSONObject();
		int status = -1;
		String message = "网络有点慢，请重试";
		Inform inform = informService.selectInformById(informID);// 根据通知id找到通知对象
		String identity = (String) session.getAttribute("identity");// 得到查看通知对象的身份

		if (identity.equals("student")) {
			String account = (String) session.getAttribute("account");// 获取学生学号
			Feedback feedback = feedbackService.selectFeedbackBySnoAndId(account, informID);
			if (feedback != null) {
				status = 200;
				message = "内容马上来,请稍后...";
				json.put("inform", inform);
				json.put("status", status);
				json.put("message", message);
				return json;
			}
		}
		if (inform != null) {// 查看文档成功

			status = 200;
			message = "内容马上来,请稍后...";
		}
		json.put("inform", inform);
		json.put("status", status);
		json.put("message", message);
		return json;
	}

	/*
	 * 修改通知
	 */
	@PostMapping("/update")
	@ResponseBody
	public JSONObject update(String informId, String title, String content) {
		JSONObject json = new JSONObject();
		int status = -1;
		String message = "通知修改失败,请重试";
		boolean result = false;
		String identity = (String) session.getAttribute("identity");

		if (identity.equals("teacher")) {
			String account = (String) session.getAttribute("account");
			result = informService.updateInformById(informId, title, content, account);
			if (result) {
				status = 200;
				message = "修改成功";
			}
		} else {
			message = "权限限制,仅老师可操作";
		}

		json.put("status", status);
		json.put("message", message);
		return json;
	}

	/*
	 * 删除通知
	 */
	@PostMapping("/delete")
	@ResponseBody
	public JSONObject deleteInformById(String informId) {
		JSONObject json = new JSONObject();
		Boolean result = false;
		String identity = (String) session.getAttribute("identity");
		if (identity.equals("teacher")) {
			String account = (String) session.getAttribute("account");
			result = informService.deleteInformById(informId, account);
		}

		int status = -1;
		String message = "通知删除失败,请重试";
		if (result) {
			status = 200;
			message = "删除成功";
		}
		json.put("status", status);
		json.put("message", message);
		return json;
	}
	/*
	 * 批量删除通知
	 */
	@PostMapping("/deleteList")
	@ResponseBody
	public JSONObject deleteInformByIdList(@RequestParam("array[]") List<String> array) {
		JSONObject json = new JSONObject();
		
		Boolean result = false;
		String identity = (String) session.getAttribute("identity");
		if (identity.equals("teacher")) {
			String account = (String) session.getAttribute("account");
			result = informService.deleteInformByIdList(array, account);
		}

		int status = -1;
		String message = "通知删除失败,请重试";
		if (result) {
			status = 200;
			message = "删除成功";
		}
		json.put("status", status);
		json.put("message", message);
		return json;
	}

	/*
	 * 学生查看通知列表（前端需要通知标题，通知标号，通知的概要内容，通知发布的时间，该学生是否已读）
	 * 流程：从session得到学生的学号id，然后根据学号查询学生表，得到学生表里面的老师工号
	 * 根据老师工号查看通知表，得到所有该老师所有的通知对象（通知对象包含通知标号，标题，内容，发布时间）
	 * 根据学生的学号id，去查询中间表feedback，得到该学生已读通知的编号id 用学生已读通知的编号和老师发布所有通知的编号比对，得出是否已读
	 */
	@PostMapping("/selectInformList")
	@ResponseBody
	public JSONObject selectInformList() {
		JSONObject json = new JSONObject();
		int status = 200;
		String message = "网络正在快马加鞭...";

		String id = (String) session.getAttribute("account");
		List<Inform> inform = informService.selectInformIdByTnoBySno(id);// 得到该学生的老师的通知
		if (inform.size() < 1) {
			status = -1;
			message = "还没有通知发布";
			json.put("status", status);
			json.put("message", message);
			return json;
		}
		List<Feedback> studentInformId = feedbackService.selectFeedbackBySno(id);// 得到该学生已读通知
		String iid = null;
		String fid = null;

		for (int i = 0; i < studentInformId.size(); i++) {
			

			fid=studentInformId.get(i).getInform().getInformId();
			for (Inform n : inform) {
				iid = n.getInformId();
				if(fid.equals(iid)){
					n.setFeedbacks(studentInformId);
				}
			}
		}
		json.put("list", inform);
		json.put("status", status);
		json.put("message", message);
		return json;
	}

}
